package com.zego.wawaji.ui.center.wawa.send;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.noble.library.basic.string.StringUtils;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.base.ExBaseAdapter;
import com.zego.wawaji.ui.bean.wawa.MyWawa;
import com.zego.wawaji.ui.bean.wawa.Wawa;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 已申请发货
 * Created by Su on 2017/11/26.
 */
public class SendWawaAdapter extends ExBaseAdapter<MyWawa> {
    private int dataLength;    // 全部数据
    private boolean isFirstPage = true;
    private Map<Integer, MyWawa> tempMap = new HashMap<>();

    public SendWawaAdapter(Context context, List<MyWawa> datas) {
        super(context, datas);
    }

    public void setDataLength(int dataLength) {
        this.dataLength = dataLength;
    }

    /**
     * 重置第一页数据, 做tempMap重新清空使用
     */
    public void resetFirstPage() {
        this.isFirstPage = true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.my_wawa_send_item);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        final MyWawa info = getItem(position);
        Wawa wawa = ModuleMgr.getWawaMgr().getWawa(info.getWwTypeId());

        holder.group_top.setVisibility(View.GONE);
        holder.group_bottom.setVisibility(View.GONE);
        holder.group_bottom1.setVisibility(View.GONE);

        if (position == 0) {           // 第一条数据
            if (isFirstPage) {
                isFirstPage = false;
                tempMap.clear();
            }
            info.setGroupNum(1);
            info.setNewGroup(false);
            refreshTop(holder, info);
        } else {
            if (tempMap.containsKey(position)) {    // 已存储过item状态
                if (info.isNewGroup() && tempMap.containsKey(position - 1)) {  // 新组
                    refreshGroup(holder, info, tempMap.get(position - 1));
                }
            } else {   // 未存储过
                if (tempMap.containsKey(position - 1)) { // 取上条item数据做对比
                    MyWawa data = tempMap.get(position - 1);
                    if (info.getTransPortID() != data.getTransPortID()) {  // 不同组
                        refreshGroup(holder, info, data);
                        info.setGroupNum(1);
                        info.setNewGroup(true);
                    } else {
                        info.setGroupNum(data.getGroupNum() + 1);
                        info.setNewGroup(false);
                    }
                }
            }
        }

        // 最后一条数据
        if (dataLength == position + 1) {
            refreshBottom(holder, info);
        }

        tempMap.put(position, info);   // 存储当前条目信息
        ImageLoader.loadRoundAvatar(getContext(), wawa.getImg(), holder.wawa_icon);
        holder.wawa_name.setText(wawa.getName());
        holder.time_info.setText(StringUtils.getCreateTime(info.getCreateTime()));
        return convertView;
    }

    /**
     * @param info 当前item信息
     * @param data 上一item信息
     */
    private void refreshGroup(MyViewHolder holder, final MyWawa info, MyWawa data) {
        holder.group_top.setVisibility(View.VISIBLE);
        holder.group_bottom.setVisibility(View.VISIBLE);

        holder.extra_info.setText("共" + data.getGroupNum() + "个娃娃");
        holder.order_num.setText("00" + info.getTransPortID());
        holder.btn_follow.setOnClickListener(v -> ModuleMgr.getWawaMgr().reqTransport(info.getTransPortID(), null));
    }

    private void refreshTop(MyViewHolder holder, final MyWawa info) {
        holder.group_top.setVisibility(View.VISIBLE);
        holder.order_num.setText("00" + info.getTransPortID());
        holder.btn_follow.setOnClickListener(v -> ModuleMgr.getWawaMgr().reqTransport(info.getTransPortID(), null));
    }

    private void refreshBottom(MyViewHolder holder, final MyWawa info) {
        holder.group_bottom1.setVisibility(View.VISIBLE);
        holder.extra_info1.setText("共" + info.getGroupNum() + "个娃娃");
    }

    private class MyViewHolder {
        private ImageView wawa_icon;
        private TextView wawa_name, time_info;

        private RelativeLayout group_top, group_bottom;
        private TextView extra_info, order_num, btn_follow;

        private RelativeLayout group_bottom1;
        private TextView extra_info1;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            wawa_icon = (ImageView) convertView.findViewById(R.id.wawa_icon);
            wawa_name = (TextView) convertView.findViewById(R.id.wawa_name);
            time_info = (TextView) convertView.findViewById(R.id.time_info);

            btn_follow = (TextView) convertView.findViewById(R.id.btn_follow);
            group_top = (RelativeLayout) convertView.findViewById(R.id.group_top);
            group_bottom = (RelativeLayout) convertView.findViewById(R.id.group_bottom);
            extra_info = (TextView) convertView.findViewById(R.id.extra_info);
            order_num = (TextView) convertView.findViewById(R.id.order_num);

            group_bottom1 = (RelativeLayout) convertView.findViewById(R.id.group_bottom1);
            extra_info1 = (TextView) convertView.findViewById(R.id.extra_info1);
        }
    }
}
